
package rendi456.swing;

import javax.swing.GroupLayout;
import javax.swing.JPanel;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;

import rendi456.swing.components.LabelField;

public class GroupLayoutUtils
{
	public static JPanel createGroupPane(LabelField[] labelFields)
	{
		SequentialGroup horizontal = null;
		SequentialGroup vertical = null;
		ParallelGroup parallel = null;
		JPanel pane = null;
		GroupLayout layout = null;
		LabelField buffer = null;

		pane = new JPanel();
		layout = new GroupLayout(pane);
		pane.setLayout(layout);

		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		// Horizontal
		horizontal = layout.createSequentialGroup();

		parallel = layout.createParallelGroup();
		for (int i = 0; i < labelFields.length; i++)
		{
			buffer = labelFields[i];
			parallel = parallel.addComponent(buffer.getLabel());
		}
		horizontal.addGroup(parallel);

		parallel = layout.createParallelGroup();
		for (int i = 0; i < labelFields.length; i++)
		{
			buffer = labelFields[i];
			parallel = parallel.addComponent(buffer.getField());
		}
		horizontal.addGroup(parallel);

		layout.setHorizontalGroup(horizontal);

		// Vertical
		vertical = layout.createSequentialGroup();

		for (int i = 0; i < labelFields.length; i++)
		{
			parallel = layout.createParallelGroup(Alignment.BASELINE);
			buffer = labelFields[i];
			parallel = parallel.addComponent(buffer.getLabel());
			parallel = parallel.addComponent(buffer.getField());
			vertical.addGroup(parallel);
		}

		layout.setVerticalGroup(vertical);

		return pane;
	}
}
